package network.editor;

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import org.apache.log4j.Logger;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxPoint;

/**
 * edge选择面板
 * 
 * @author zhangzuoqiang <br/>
 *         Date: Aug 25, 2011
 */
public class EditorEdgePanel extends AbstarctEditorPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(EditorEdgePanel.class);

	public EditorEdgePanel() {
		init();
	}

	private void init() {
		GeoResource edgeResource = EditorResource.getEdgeResource();
		int edgeNodeCount = edgeResource.getNames().size();// 获得图片的数量
		if (edgeNodeCount > 0) {
			setLayout(new GridLayout(0, 3));// 3列，任意行
			for (int i = 0; i < edgeNodeCount; i++) {
				ImageIcon sourceIcon = (ImageIcon) edgeResource.getIcons().get(
						i);// 边的图标
				// 边的尺寸默认是100*100,边不显示文字，所以带一个“”的对象
				addEdge(edgeResource.getNames().get(i), sourceIcon,
						edgeResource.getStyles().get(i), 100, 100, "");
			}
		}
	}

	public void addEdge(final String name, ImageIcon icon, String style,
			int width, int height, Object value) {
		mxGeometry geometry = new mxGeometry(0, 0, width, height);
		geometry.setTerminalPoint(new mxPoint(0, height), true);
		geometry.setTerminalPoint(new mxPoint(width, 0), false);
		geometry.setRelative(true);

		mxCell cell = new mxCell(value, geometry, style);
		cell.setEdge(true);

		addImageLabel(name, icon, cell);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(3);
		frame.getContentPane().add(new EditorEdgePanel());
		frame.setVisible(true);

		frame.pack();
	}
}